﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Hxsoft.AppV1.Module;

namespace Hxsoft.AppV1.WebUI
{
	/// <summary>
	/// 文章列表。
	/// </summary>
	public class ArticleList : BaseRepeaterList
	{
		protected const string ModuleCodeSeparator = ",";

		private LoadedValue<string> loadedModuleCode;
		/// <summary>
		/// 列表的文章模块代码。
		/// </summary>
		public virtual string ModuleCode
		{
			get
			{
				if (loadedModuleCode.Loader == null)
					loadedModuleCode.Loader = () => (string)ViewState["ModuleCode"];
				return loadedModuleCode.Value;
			}
			set
			{
				if (loadedModuleCode.Setter == null)
					loadedModuleCode.Setter = v => ViewState["ModuleCode"] = v;
				loadedModuleCode.Value = value;
			}
		}

		private LoadedValue<Module.Module> loadedModuleInfo;
		public Module.Module ModuleInfo
		{
			get
			{
				if (loadedModuleInfo.Loader == null)
					loadedModuleInfo.Loader = () => SiteId.HasValue ? Modules.GetItem(SiteId.Value, ModuleCode) : null;
				return loadedModuleInfo.Value;
			}
		}

		private LoadedValue<string> loadedAttachedModuleCodes;
		/// <summary>
		/// 附加的文章模块代码，附加多个模块请用","分隔。
		/// </summary>
		public virtual string AttachedModuleCodes
		{
			get
			{
				if (loadedAttachedModuleCodes.Loader == null)
					loadedAttachedModuleCodes.Loader = () => (string)ViewState["AttachedModuleCodes"];
				return loadedAttachedModuleCodes.Value;
			}
			set
			{
				if (loadedAttachedModuleCodes.Setter == null)
					loadedAttachedModuleCodes.Setter = v => ViewState["AttachedModuleCodes"] = v;
				loadedAttachedModuleCodes.Value = value;
			}
		}

		private LoadedValue<string> loadedKeyword;
		/// <summary>
		/// 用于检索列表的关键字。
		/// </summary>
		public virtual string Keyword
		{
			get
			{
				if (loadedKeyword.Loader == null)
					loadedKeyword.Loader = () => (string)ViewState["Keyword"];
				return loadedKeyword.Value;
			}
			set
			{
				if (loadedKeyword.Setter == null)
					loadedKeyword.Setter = v => ViewState["Keyword"] = v;
				loadedKeyword.Value = value;
			}
		}

		private LoadedValue<ArticleTitleType> loadedTitleType;
		/// <summary>
		/// 列表的标题类型（长、短、中）。
		/// </summary>
		public virtual ArticleTitleType TitleType
		{
			get
			{
				if (loadedTitleType.Loader == null)
					loadedTitleType.Loader = () => EnumExtensions.ConvertFromOrDefault<ArticleTitleType>(ViewState["TitleType"], ArticleTitleType.Title);
				return loadedTitleType.Value;
			}
			set
			{
				if (loadedTitleType.Setter == null)
					loadedTitleType.Setter = v => ViewState["TitleType"] = v;
				loadedTitleType.Value = value;
			}
		}

		/// <summary>
		/// 列表的模块ID。
		/// </summary>
		public int? ModuleId
		{
			get
			{
				if(ModuleInfo == null) return null;
				return ModuleInfo.ModuleId;
			}
		}



		private LoadedValue<string> loadedListTitle;
		/// <summary>
		/// 列表的标题，如不进行设置，则使用模块的名称。
		/// </summary>
		public override string ListTitle
		{
			get
			{
				if (loadedListTitle.Loader == null)
					loadedListTitle.Loader = () => (string)ViewState["ListTitle"];
				return string.IsNullOrEmpty(loadedListTitle.Value) ? Modules.GetNameById(SiteId, ModuleCode) : loadedListTitle.Value;
			}
			set
			{
				if (loadedListTitle.Setter == null)
					loadedListTitle.Setter = v => ViewState["ListTitle"] = v;
				loadedListTitle.Value = value;
			}
		}

		private LoadedValue<string> loadedListMoreUrl;
		/// <summary>
		/// 列表更多的Url地址。
		/// </summary>
		public override string ListMoreUrl
		{
			get
			{
				if (loadedListMoreUrl.Loader == null)
					loadedListMoreUrl.Loader = () => (string)ViewState["ListMoreUrl"];
				return string.IsNullOrEmpty(loadedListMoreUrl.Value) ? GenericUrlManager.BuildArticleListUrl(ModuleId, null) : loadedListMoreUrl.Value;
			}
			set
			{
				if (loadedListMoreUrl.Setter == null)
					loadedListMoreUrl.Setter = v => ViewState["ListMoreUrl"] = v;
				loadedListMoreUrl.Value = value;
			}
		}

		/// <summary>
		/// 标题字段名，根据 <see cref="TitleType"/> 决定。
		/// </summary>
		public override string TitleField
		{
			get
			{
				switch (TitleType)
				{
					case ArticleTitleType.ShortTitle: return "ShortTitle";
					case ArticleTitleType.MiddleTitle: return "MiddleTitle";
				}
				return "Title";
			}
		}

		/// <summary>
		/// 日期字段名。
		/// </summary>
		public override string DateTimeField
		{
			get
			{
				return "ReleaseDate";
			}
		}

		/// <summary>
		/// 每一个列表项的Url。
		/// </summary>
		public override string ItemUrl
		{
			get { return GenericUrlManager.BuildArticleDetailUrl((int)Eval("ModuleId"), (int)Eval("ArticleId")); }
		}

		/// <summary>
		/// 图片资源字段。
		/// </summary>
		public override string ImageResourceField
		{
			get
			{
				return "ImageType";
			}
		}

		/// <summary>
		/// 当使用附加模块的，标识一个列表项所属的模块名称。
		/// </summary>
		public virtual string ItemModuleName
		{
			get { return Eval("ModuleName", null); }
		}

		/// <summary>
		/// 获取列表数据。
		/// </summary>
		/// <returns>获取的列表数据。</returns>
		public override object GetListData()
		{
			if (!string.IsNullOrEmpty(AttachedModuleCodes))
			{
				List<string> moduleCodes = new List<string>();
				if (!string.IsNullOrEmpty(ModuleCode)) moduleCodes.Add(ModuleCode);
				moduleCodes.AddRange(AttachedModuleCodes.Split(ModuleCodeSeparator, StringSplitOptions.RemoveEmptyEntries));
				return Articles.GetBaseList(SiteId, moduleCodes.ToArray(), Keyword, LoadCount);
			}

			return Articles.GetBaseList(SiteId, ModuleId, Keyword, LoadCount);
		}
	}
}